package com.imoresoft.magic.parser.templet;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.imoresoft.magic.property.AppConstants;
import com.imoresoft.magic.property.ProsReader;

public class UserTempletParser1 extends IUserTempletParser {

	public UserTempletParser1(SuperDao superDao, IData userTempletStyle,
			IDataset itemList, IData buyerData, String title, int numPerTr)
			throws Exception {
		super(superDao, userTempletStyle, itemList, buyerData, title, numPerTr);
	}

	@Override
	public String buildDisplayHtml() throws Exception {
		String frameCode = templetCode.getString("FRAME_CODE");

		// 替换search code
		String firstTrCode = templetCode.getString("FIRST_TR_CODE");
		frameCode = frameCode.replace("#{first_tr_code}", firstTrCode);

		String secondTrCode = buildSecondTr();
		frameCode = frameCode.replace("#{second_tr_code}", secondTrCode);

		String thirdTrCode = templetCode.getString("THIRD_TR_CODE");
		frameCode = frameCode.replace("#{third_tr_code}", thirdTrCode);

		// 替换其它所有的@{}变量
		return replaceAllGloableVariables(frameCode);
	}

	private String buildSecondTr() throws Exception {
		String secondTrCode = templetCode.getString("SECOND_TR_CODE");
		return secondTrCode.replace("#{item_code}", buildCode4AllItems());
	}

	private String buildCode4AllItems() throws Exception {
		StringBuilder prModuleHTML4AllItems = new StringBuilder("");
		for (int i = 0; i < itemList.size(); i++) {
			IData itemToBePromoted = itemList.getData(i);
			String proModuleHTML4OneItem = buildProModule4OneItem(itemToBePromoted);
			prModuleHTML4AllItems.append(proModuleHTML4OneItem);
		}
		return prModuleHTML4AllItems.toString();
	}

	private String buildProModule4OneItem(IData itemToBePromoted) {
		String itemContentCode = templetCode.getString("ITEM_CODE");
		// 宝贝IID
		itemContentCode = itemContentCode.replace("@{iid}", itemToBePromoted
				.getString("IID"));

		// 替换宝贝图片地址
		String picUrl = itemToBePromoted.getString("PIC_URL");
		String picType = ProsReader.getString("PIC_TYPE_" + numPerTr);
		picUrl += picType;
		itemContentCode = itemContentCode.replace("@{pic_url}", picUrl);

		// 替换宝贝名称
		String itemTitle = itemToBePromoted.getString("TITLE");
//		if (itemTitle.length() > 18) {
//			itemTitle = itemTitle.substring(0, 18) + "··";
//		}
		itemContentCode = itemContentCode.replace("@{item_title}", itemTitle);

		// 替换价格
		String price = itemToBePromoted.getString("PRICE");
		itemContentCode = itemContentCode.replace("@{price}", price);

		return itemContentCode;
	}

	private String replaceAllGloableVariables(String frameCode) {
		frameCode = StringUtils.replace(frameCode, "@{title}", tilte);

		String welcome = userTempletStyle.getString("WELCOME");
		frameCode = StringUtils.replace(frameCode, "@{welcome}", welcome);

		String shopUrl = "#";
		frameCode = StringUtils.replace(frameCode, "@{shop_url}", shopUrl);

		String buyerNick = buyerData.getString("BUYER_NICK");
		if (StringUtils.isBlank(buyerNick)) {
			buyerNick = "亲";
		}
		frameCode = StringUtils.replace(frameCode, "@{buyer_nick}", buyerNick);

		// 替换天气信息
		String weatherCode = StringUtils.EMPTY;
		if (AppConstants.FLAG_YES.equals(userTempletStyle.getString("IS_WEATHER"))) {
			weatherCode = super.templetCode.getString("WEATHER_CODE");
		}
		frameCode = StringUtils.replace(frameCode,
				"#{weather_code}", weatherCode);
		
		// 替换背景图片
		frameCode = StringUtils.replace(frameCode, "@{top_bg_img}", templetSkin
				.getString("TOP_BG_IMG"));
		// 替换标题区域左圆角图片
		frameCode = StringUtils.replace(frameCode, "@{top_left_img}",
				templetSkin.getString("TOP_LEFT_IMG"));
		// 替换标题区域背景图片
		frameCode = StringUtils.replace(frameCode, "@{top_middle_img}",
				templetSkin.getString("TOP_MIDDLE_IMG"));
		// 替换标题区域右圆角图片
		frameCode = StringUtils.replace(frameCode, "@{top_right_img}",
				templetSkin.getString("TOP_RIGHT_IMG"));

		// 替换item_td的高和宽度
		String itemSize = userTempletStyle.getString("ITEM_SIZE");
		frameCode = StringUtils.replace(frameCode, "@{td_height}", itemSize);
		frameCode = StringUtils.replace(frameCode, "@{td_width}", itemSize);

		String pro4bbbb = "Powered by 百变宝贝";
		frameCode = frameCode.replace("@{pro4bbbb}", pro4bbbb);

		return frameCode;
	}
}
